﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameName1
{
    class TextureHandler
    {
        private Texture2D m_tex;

        public enum MOVE_STATE { UNDEF = 0, STANDINGL, STANDINGR, MOVINGL, MOVINGR };
        public enum HEALTH_STATE { UNDEF = 0, HEALTHY, HURT };

        HEALTH_STATE h_state = HEALTH_STATE.HEALTHY;
        MOVE_STATE m_state = MOVE_STATE.STANDINGL;

        const double HURT_TIMER = 0.05f;
        double m_hurttimer = 0.0f;
        int m_spriteindex = 0;
        Color m_color = Color.White;

        int m_currentframe = 0;
        static int m_rows = 4;
        static int m_columns = 9;
        int m_totalframes = m_columns * m_rows;
        int m_width;
        int m_height;

        Rectangle m_source;

        const int FRAME_DELAY = 8;
        int m_framedelay = 0;


        TextureHandler(Texture2D i_tex)
        {
            m_tex = i_tex;
            m_width = m_tex.Width / m_columns;
            m_height = m_tex.Height / m_rows;
        }

        public TextureHandler()
        {

        }

        public Texture2D Texture
        {
            get { return m_tex; }
            set { m_tex = value;
                  m_width = m_tex.Width / m_columns;
                  m_height = m_tex.Height / m_rows;
                }
        }

        public Rectangle SourceRect
        {
            get { return m_source; }
        }

        public int CurrFrame
        {
            get { return m_currentframe; }
            set { m_currentframe = value; }
        }

        public int Width
        {
            get { return m_width; }
        }

        public int Height
        {
            get { return m_height; }
        }

        public Color TexColor
        {
            get { return m_color; }
        }

        public HEALTH_STATE HealthState
        {
            get { return h_state; }
            set { 
                   h_state = value;

                   if (value == HEALTH_STATE.HURT)
                   {
                       m_hurttimer = HURT_TIMER;
                       m_color = Color.Red;
                   }
                   else
                   {    
                       m_color = Color.White;
                   }
                }
        }

        public MOVE_STATE MoveState
        {
            get { return m_state; }
            set { 

                    MOVE_STATE p = m_state;
                    m_state = value;
                    switch (m_state)
                    {
                        case MOVE_STATE.STANDINGL:
                            m_spriteindex = 9;
                            break;
                        case MOVE_STATE.STANDINGR:
                            m_spriteindex = 27;
                            break;
                        case MOVE_STATE.MOVINGL:
                            m_spriteindex = 9;
                            break;
                        case MOVE_STATE.MOVINGR:
                            m_spriteindex = 27;
                            break;
                        default:
                            break;
                    }

                    if (m_state != p)
                    {
                        m_currentframe = m_spriteindex;
                    }
            
                }
        }

        public void Update(double t)
        {
            if (m_state == MOVE_STATE.MOVINGL || m_state == MOVE_STATE.MOVINGR)
                Increment();
            else
            {
                m_currentframe = m_spriteindex;
                UpdateRectangle();
            }

            if (h_state == HEALTH_STATE.HEALTHY)
                return;

            if (h_state == HEALTH_STATE.HURT)
            {
                m_hurttimer -= t;
                if (m_hurttimer < 0)
                {
                    HealthState = HEALTH_STATE.HEALTHY;
                }
            }
        }

        private void Increment()
        {
            m_framedelay++;
            if (m_framedelay < FRAME_DELAY)
            {
                return;
            }
            m_currentframe++;

            if (m_currentframe == m_spriteindex + (m_columns-1))
                m_currentframe = m_spriteindex;


            m_framedelay = 0;
            ///TO DO: SELECT CORRECT FRAME
            UpdateRectangle();

           
        }

        private void UpdateRectangle()
        {
            int row = (int)((float)m_currentframe / (float)m_columns);
            int column = m_currentframe % m_columns;

            m_source = new Rectangle(m_width * column, m_height * row, m_width, m_height);
        }

    }
}
